﻿<%@ WebHandler Language="C#" Class="request" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
public class request : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        //context.Response.Write();
        string cmd = context.Request.Form["cmd"];
        if (cmd == "photo")
        {
            string gongyu = context.Request.Form["gongyu"].ToString();
            string pageSize = context.Request.Form["pagesize"].ToString();
            string sql = "select top " + pageSize + " 姓名 as name,班级 as banji,专业 as zhuanye,寝室号 as qinshihao,photo,资助与奖励 as msg1,获奖情况 as msg2,xuehao from stu where photo is not null and 公寓楼='" + gongyu + "' and CHARINDEX('号',公寓楼)<>0 order by NewID()";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            context.Response.End();
            return;
        }
        if (cmd == "randsum")
        {
            Int32 pageSize =  Convert.ToInt16(context.Request.Form["pagesize"]);
            string sql = "select top "+pageSize*4+" 姓名 as name,班级 as banji,专业 as zhuanye,寝室号 as qinshihao from stu order by newid()";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            context.Response.End();
            return;
        }
        if (cmd == "postifmt")
        {
            int fid = Convert.ToInt16(context.Request.Form["fid"]);
            if (SQLHelper.GetReader("select * from fdycj where fid=" + fid).Read())
            {
                context.Response.Write("你已经参加过考核了，请勿重复提交！");
                context.Response.End();
                return;
            }
            double zongfen = 0;
            context.Response.ContentType = "text/plain";
            string arr = context.Request.Form["data"];
            arr = arr.Substring(1, arr.Length - 2).Replace("\"", "");
            string[] myarr = arr.Split(',');
            for (int i = 0; i < 20; i++)
            {
                if (myarr[i] == "null")
                {
                    continue;
                }
                else
                {
                    string[] ifmt = myarr[i].Split('-');
                    string imgsrc = ifmt[0].Replace("\\\\", "\\");//照片地址
                    string xm = ifmt[1];//姓名
                    string bj = ifmt[2];//班级
                    string zy = ifmt[3];//专业
                    string qs = ifmt[4];//寝室号
                    string zx = ifmt[5];//在校担任
                    string gx = ifmt[6];//重点关注
                    string pp = ifmt[7];//个人信息
                    DataTable db = SQLHelper.GetTable("select * from stu where photo='" + imgsrc+"'");
                    //string test = db.Rows[0][1].ToString() + db.Rows[0][3].ToString() + db.Rows[0][6].ToString() + db.Rows[0][8].ToString();
                    if (db.Rows[0][1].ToString() == xm)
                    {
                        zongfen++;
                    }
                    if (db.Rows[0][3].ToString() == bj)
                    {
                        zongfen++;
                    }
                    if (db.Rows[0][6].ToString() == zy)
                    {
                        zongfen++;
                    }
                    if (db.Rows[0][8].ToString() == qs)
                    {
                        zongfen++;
                    }
                    //if (SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and 姓名='"+xm+"'").Rows.Count>0)
                    //{
                    //    zongfen++;
                    //}
                    //if (SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and 班级='" + bj + "'").Rows.Count > 0)
                    //{
                    //    zongfen++;
                    //    //zongfen=zongfen+0.5;
                    //}
                    //if (SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and 专业='" + zy + "'").Rows.Count > 0)
                    //{
                    //    zongfen++;
                    //    //zongfen = zongfen + 0.5;
                    //}
                    //if (SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and 寝室号='" + qs + "'").Rows.Count > 0)
                    //{
                    //    zongfen++;
                    //}
                    //在校担任计分
                    string[] zxx = zx.Split('|');
                    string dbug = db.Rows[0][9].ToString();
                    if (zxx[1] == db.Rows[0][9].ToString())
                    {
                        zongfen = zongfen + 0.2;
                    }

                    if (zxx[2] == db.Rows[0][10].ToString())
                    {
                        zongfen = zongfen + 0.2;
                    }

                    if (zxx[3] == db.Rows[0][11].ToString())
                    {
                        zongfen = zongfen + 0.2;
                    }
                    
                    //if (zxx[1].ToString() == "" && SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and 班级干部 is null").Rows.Count > 0)
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                    //else if (zxx[1] == SQLHelper.GetTable("select 班级干部 from stu where photo='" + imgsrc + "'").Rows[0][0].ToString())
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                    
                    //if (zxx[2].ToString() == "" && SQLHelper.GetReader("select * from stu where photo='" + imgsrc + "' and 分院干部 is null").Read())
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}else if (zxx[2] == SQLHelper.GetTable("select 分院干部 from stu where photo='" + imgsrc + "'").Rows[0][0].ToString())
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                    
                    //if (zxx[3].ToString() == "" && SQLHelper.GetReader("select * from stu where photo='" + imgsrc + "' and 校级干部 is null").Read())
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}else if (zxx[3] == SQLHelper.GetTable("select 校级干部 from stu where photo='" + imgsrc + "'").Rows[0][0].ToString())
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                    //重点关注计分
                    string[] gxx = gx.Split('|');
                    
                    string pinkun = db.Rows[0][26].ToString();
                    if (gxx[1].ToString() == pinkun)
                    {
                        zongfen = zongfen + 0.2;
                    }
                    else if (gxx[1].ToString() != "" && pinkun!="")
                    {
                        zongfen = zongfen + 0.2;
                    }
                    
                    if (gxx[2].ToString() == pinkun)
                    {
                        zongfen = zongfen + 0.2;
                    }
                    else if (gxx[2].ToString() != "" && pinkun != "")
                    {
                        zongfen = zongfen + 0.2;
                    }
                    //if (gxx[1].ToString() == "" && SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and pinkun is null").Rows.Count>0)
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                    //else if (gxx[1].ToString() != "" && SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and pinkun is not null").Rows.Count > 0)
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                    //if (gxx[2].ToString() == "" && SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and pinkun is null").Rows.Count > 0)
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                    //else if (gxx[2].ToString() != "" && SQLHelper.GetTable("select * from stu where photo='" + imgsrc + "' and pinkun is not null").Rows.Count > 0)
                    //{
                    //    zongfen = zongfen + 0.2;
                    //}
                }
            }
            string sql = string.Format("insert into fdycj values({0},{1},'{2}')",fid,zongfen,DateTime.Now.ToLocalTime());
            SQLHelper.ExcuteSQL(sql);
            context.Response.Write("本次考核得分：" + Math.Round(zongfen,1) + " 分");
            context.Response.End();
            return;
        }
        //管理员
        if (cmd == "adminlogin")//登录
        {
            string u = context.Request.Form["u"];
            string p = context.Request.Form["p"];
            string sql = string.Format("select * from admin where username='{0}' and password='{1}'", u, p);
            if (SQLHelper.GetReader(sql).Read())
            {
                context.Response.Write("success");
            }
            else
            {
                context.Response.Write("error");
            }
            context.Response.End();
            return;
        }
        //更新学生信息
        if (cmd == "stuupdate")
        {
            string uid = context.Request.Form["uid"];
            string xingming = context.Request.Form["xingming"];
            string nianji = context.Request.Form["nianji"];
            string banji = context.Request.Form["banji"];
            string xuehao = context.Request.Form["xuehao"];
            string xueyuan = context.Request.Form["xueyuan"];
            string zhuanye = context.Request.Form["zhuanye"];
            string gongyulou = context.Request.Form["gongyulou"];
            string qinshihao = context.Request.Form["qinshihao"];
            string jiguan = context.Request.Form["jiguan"];
            string zizhu = context.Request.Form["zizhu"];
            string huojiang = context.Request.Form["huojiang"];
            string sql = string.Format("update stu set 姓名='{0}',年级='{1}',班级='{2}',xuehao='{3}',学院='{4}',专业='{5}',公寓楼='{6}',寝室号='{7}',资助与奖励='{8}',获奖情况='{9}',籍贯='{10}' where ID={11}",xingming,nianji,banji,xuehao,xueyuan,zhuanye,gongyulou,qinshihao,zizhu,huojiang,jiguan,uid);
            if (SQLHelper.ExcuteSQL(sql) > 0)
            {
                context.Response.Write("保存成功");
            }
            else
            {
                context.Response.Write("保存失败");
            }
            context.Response.End();
            return;
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}